<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
    <% String path=request.getContextPath(); String basePath=request.getScheme() + "://" + request.getServerName() + ":"
        + request.getServerPort() + path + "/" ; %>

        <!DOCTYPE html>
        <html>

        <head>
            <base href="<%=basePath%>">

            <title>字典管理</title>

            <meta http-equiv="pragma" content="no-cache">
            <meta http-equiv="cache-control" content="no-cache">
            <meta http-equiv="expires" content="0">
            <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
            <meta http-equiv="description" content="This is my page">
            <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->

        </head>

        <body>
            <%@ include file="/WEB-INF/jsp/common/header.jsp" %>
                <%@ include file="/WEB-INF/jsp/common/loadDiv.jsp" %>

                    <script>
                        $(function () {
                            //alert(test);
                            loadCombotree();
                            bindtreegrid();
                        });

                    </script>
                    <script>
                        function editrow() {
                            var node = $('#tt').treegrid('getSelected');
                            if (node) {
                                if (node.dicParentId != '0') {
                                    $.messager.alert('消息', '该按钮仅支持更改字典类');
                                    return
                                }
                                //修改
                                $("#dicId").val(node.dicId);
                                $('#dicName').val(node.dicName);
                                $('#remark').val(node.remark);
                                //$('#t1').attr("disabled", "disabled");
                                $('#dicSort').numberbox('setValue', node.dicSort);
                                $('#add').window('open');
                            } else {
                                $.messager.alert('消息', '请选择一个字典名称');
                            }
                        }

                        function editrowItem() {
                            var node = $('#tt').treegrid('getSelected');
                            console.log('000000', node)
                            if (node) {
                                if (node.dicParentId == '0') {
                                    $.messager.alert('消息', '该按钮仅支持更改字典子项');
                                    return
                                }
                                //修改
                                $("#item_dicId").val(node.dicId);
                                $('#item_dicName').val(node.dicName);
                                $('#item_dicValue').val(node.dicValue);
                                $('#item_remark').val(node.remark);
                                $('#par_dict').combotree('setValue', node.dicParentId);
                                $('#par_dict').combotree('enable');
                                //$('#t1').attr("disabled", "disabled");
                                $('#item_dicSort').numberbox('setValue', node.dicSort);
                                $('#addDictItem').window('open');
                            } else {
                                $.messager.alert('消息', '请选择一个字典名称');
                            }
                        }

                        function save() {
                            if (!$('#ssec-form').form('validate')) {
                                return;
                            }
                            var node = $('#tt').treegrid('getSelected');
                            var dicId = $('#dicId').val();
                            var dicName = $('#dicName').val();
                            var dicSort = $('#dicSort').val();
                            var status = $('#status').combobox('getValue');
                            var remark = $('#remark').val();
                            $.ajax({
                                type: "post",
                                url: "sysDict/insert",
                                data: {
                                    dicId: dicId,
                                    dicName: dicName,
                                    dicSort: dicSort,
                                    status: status,
                                    remark: remark,
                                    userName: PUser.loginname,
                                    userId: PUser.userid
                                },
                                success: function (data) {
                                    $.messager.alert('消息', data);
                                    if (data == '保存成功') {
                                        $('#add').window('close');
                                        bindtreegrid();
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    $.messager.alert('消息', errorThrown);
                                }
                            });
                        }

                        function saveItemDict() {
                            if (!$('#ssec-form-item').form('validate')) {
                                return;
                            }
                            var node = $('#tt').treegrid('getSelected');
                            var dicId = $('#item_dicId').val();
                            var dicSort = $('#item_dicSort').val();
                            var status = $('#item_status').combobox('getValue');
                            var remark = $('#item_remark').val();
                            var dicParentId = $('#par_dict').combotree('getValue');
                            var dicName = $('#item_dicName').val();
                            var dicValue = $('#item_dicValue').val();
                            $.ajax({
                                type: "post",
                                url: "sysDict/insertItemDict",
                                data: {
                                    dicId: dicId,
                                    dicName: dicName,
                                    dicSort: dicSort,
                                    status: status,
                                    remark: remark,
                                    userName: PUser.loginname,
                                    userId: PUser.userid,
                                    dicParentId: dicParentId,
                                    dicValue: dicValue
                                },
                                success: function (data) {
                                    $.messager.alert('消息', data);
                                    if (data == '保存成功') {
                                        $('#addDictItem').window('close');
                                        bindtreegrid();
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    $.messager.alert('消息', errorThrown);
                                }
                            });
                        }

                        function del(node) {
                            $.ajax({
                                type: "post",
                                url: "sysDict/delDictClassesByPrimaryKey",
                                data: {
                                    dicId: node.dicId,
                                    userName: PUser.loginname,
                                    userid: PUser.userid
                                },
                                success: function (data) {
                                    $.messager.alert('消息', data);
                                    $('#add').window('close');
                                    bindtreegrid();
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    $.messager.alert('消息', errorThrown);
                                }
                            });
                        }
                    </script>
                    <script type="text/javascript">
                        function loadCombotree() {
                            $('#par_dict').combotree({
                                url: 'sysDict/DictListJson_combobox',
                                onLoadSuccess: function () {
                                    var t = $('#par_dict').combotree('tree');	// get the tree object
                                    var n = t.tree('collapseAll');		// get selected node
                                }
                            });
                            
                        }

                        function bindtreegrid() {
                            $('#tt')
                                .treegrid(
                                    {
                                        title: '数据字典列表',
                                        url: 'sysDict/selectTreeList',
                                        iconCls: 'icon-application',
                                        nowrap: true,
                                        width: 1000,
                                        height: 415,
                                        rownumbers: true,
                                        idField: 'dicId',
                                        treeField: 'dicName',
                                        columns: [[{
                                            field: 'dicId',
                                            title: 'id',
                                            formatter: function (val, rec) {
                                                return unescape(val)
                                            }
                                        }, {
                                            field: 'dicName',
                                            title: '字典名称',
                                            width: 300,
                                            formatter: function (val, rec) {
                                                return val
                                            }
                                        },
                                        /* {
                                         field: 'dicName',
                                         title: '字典项',
                                         sortable: true,
                                         width: 200,
                                         formatter: function (val, rec) {
                                             return unescape(val)
                                         }
                                     }, */
                                        {
                                            field: 'dicValue',
                                            title: '字典值',
                                            sortable: true,
                                            width: 100,
                                            formatter: function (val, rec) {
                                                return unescape(val)
                                            }
                                        }, {
                                            field: 'dicSort',
                                            title: '排序',
                                            sortable: true,
                                            width: 100,
                                            formatter: function (val, rec) {
                                                return unescape(val)
                                            }
                                        }, {
                                            field: 'status',
                                            title: '启用/禁用',
                                            sortable: true,
                                            width: 100,
                                            formatter: function (val, rec) {
                                                return unescape(rec.status == '1' ? '启用' : '禁用')
                                            }
                                        }, {
                                            field: 'remark',
                                            title: '备注',
                                            sortable: true,
                                            width: 100,
                                            formatter: function (val, rec) {

                                                return unescape(val)
                                            }
                                        }/*,
                        {
                            title: '操作',
                            field: 'id',
                            width: 300,
                            align: 'center',
                            formatter: function (val, rec) {
                                var btn = '<a class="detail" onclick="del(\'' + rec + '\')" href="javascript:void(0)">删除</a>';
                                // var btn = "<a href='#' class='easyui-menubutton' data-options=\'menu:'#mm1',iconCls:'icon-edit'/>";
                                return btn;
                            }
                        }*/
                                        ]],
                                        toolbar: [
                                            {
                                                text: '新增字典类',
                                                //disabled: Permission.indexOf('显示') > 0 ? false : true,
                                                iconCls: 'icon-add',
                                                handler: function () {
                                                    $('#ssec-form').form('clear');
                                                    var node = $('#tt').treegrid(
                                                        'getSelected');
                                                    loadCombotree();
                                                    $('#add').window('open');
                                                }
                                            },
                                            '-',
                                            {
                                                text: '修改字典类',
                                                iconCls: 'icon-edit',
                                                handler: function () {
                                                    editrow();
                                                }
                                            },
                                            '-',
                                            {
                                                text: '新增字典子项',
                                                iconCls: 'icon-add',
                                                handler: function () {
                                                    $('#ssec-form-item').form('clear');
                                                    var node = $('#tt').treegrid(
                                                        'getSelected');
                                                    if (node != null) {
                                                        //弹出窗口
                                                        /* if (node.dicId == PUser.deptid) {
                                                             $.messager.alert('消息',
                                                                 "不可以在本级增加同级机构");
                                                             return;
                                                         }*/
                                                        $('#par_dict').combotree('setValue', node.dicId);
                                                    }
                                                    $('#addDictItem').window('open');
                                                }
                                            },
                                            '-',
                                            {
                                                text: '修改字典子项',
                                                iconCls: 'icon-edit',
                                                handler: function () {
                                                    editrowItem();
                                                }
                                            }, '-',
                                            {
                                                text: '删除字典',
                                                iconCls: 'icon-remove',
                                                handler: function () {
                                                    var node = $('#tt').treegrid(
                                                        'getSelected');
                                                    if (node) {
                                                        $.messager
                                                            .confirm(
                                                                '提示',
                                                                '会删除此项及下属所有子项，确定要删除【'
                                                                + unescape(node.dicName)
                                                                + '】这个字典吗?',
                                                                function (
                                                                    r) {
                                                                    if (r) {
                                                                        del(node);
                                                                    }
                                                                });
                                                    } else
                                                        $.messager.alert('消息',
                                                            '请选中要删除的字典');
                                                }
                                            }, '-',
                                        ],
                                        onLoadSuccess: function () {
                                            // $('#tt').treegrid('collapseAll');
                                            // var rows = $('#tt').treegrid('getChildren');
                                            // if (rows.length) {
                                            //     $('#tt').treegrid('expand',
                                            //         rows[0].deptid);
                                            // }
                                        }
                                    });
                        }
                    </script>

                    <style scoped="scoped">
                        .textbox {
                            height: 20px;
                            margin: 0;
                            padding: 0 2px;
                            box-sizing: content-box;
                        }

                        .table tr {
                            height: 30px;
                        }
                    </style>
                    <div class="query_criteria" id="query_criteria">
                        <table id="tt" style="height: 500px;"></table>
                        <div id="add" class="easyui-window" closed="true" modal="true" maximized="false" title="字典类"
                            style="width: 500px; height: 400px; padding: 10px 20px">
                            <form id="ssec-form" class="ssec-form">
                                <table class="table">
                                    <tr>
                                        <td>
                                            <div>
                                                字典名称：<input id="dicId" type="hidden"
                                                    class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <input id="dicName" class="easyui-validatebox textbox"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">排序：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="dicSort" class="easyui-numberbox" style="width: 50px;"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">启用状态：</div>
                                        </td>
                                        <td><select id="status" class="easyui-combobox" style="width: 100px;"
                                                data-options="required:true">
                                                <option value="1">启用</option>
                                                <option value="0">禁用</option>
                                            </select></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">备注：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="remark" class="easyui-validatebox textbox"
                                                    data-options="required:false" />
                                            </div>
                                        </td>
                                    </tr>

                                </table>
                                <div style="padding: 5px; text-align: right;">
                                    <a class="easyui-linkbutton" iconcls="icon-save" onclick="save();">保存</a>
                                </div>
                            </form>
                        </div>

                        <div id="addDictItem" class="easyui-window" closed="true" modal="true" maximized="false"
                            title="字典子项" style="width: 500px; height: 400px; padding: 10px 20px">
                            <form id="ssec-form-item" class="ssec-form">
                                <table class="table">
                                    <tr>
                                        <td>
                                            <div class="ssec-label">
                                                所属字典类：<input id="dicParentId" type="hidden"
                                                    class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <input id="par_dict" class="easyui-combotree"
                                                    data-options="required:true" style="width: 230px" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div>
                                                字典名称：<input id="item_dicId" type="hidden"
                                                    class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <input id="item_dicName" class="easyui-validatebox textbox"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div>字典值：</div>
                                        </td>
                                        <td>
                                            <div>
                                                <input id="item_dicValue" class="easyui-validatebox textbox"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">排序：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="item_dicSort" class="easyui-numberbox"
                                                    data-options="required:true" style="width:50px;" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">启用状态：</div>
                                        </td>
                                        <td><select id="item_status" class="easyui-combobox" style="width: 100px;"
                                                data-options="required:true">
                                                <option value="1">启用</option>
                                                <option value="0">禁用</option>
                                            </select></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">备注：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="item_remark" class="easyui-validatebox textbox"
                                                    data-options="required:false" />
                                            </div>
                                        </td>
                                    </tr>

                                </table>
                                <div style="padding: 5px; text-align: right;">
                                    <a class="easyui-linkbutton" iconcls="icon-save" onclick="saveItemDict();">保存</a>
                                </div>
                            </form>
                        </div>
                    </div>
        </body>

        </html>